package cn.spiral.qz_job.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.spiral.core.action.BaseAction;
import cn.spiral.core.constant.Constant;
import cn.spiral.core.utils.PageResult;
import cn.spiral.core.utils.QueryHelper;
import cn.spiral.qz_job.entity.QzJob;
import cn.spiral.qz_job.service.QzJobService;
import cn.spiral.user.entity.User;

public class QzJobAction extends BaseAction{

	@Autowired
	private QzJobService qzJobService;
	
	private QzJob qzJob;
	private List<QzJob> list;
	
	private Object result;
	
	private File sendExcel;
	private String sendExcelContentType;
	private String sendExcelFileName;
	
	public String listUI(){
		
		ActionContext.getContext().getContextMap().put("jobSendTypeMap", qzJob.JOB_SEND_TYPE_MAP);
		ActionContext.getContext().getContextMap().put("jobStateMap", qzJob.JOB_STATE_MAP);
		
		QueryHelper queryHelper=new QueryHelper(QzJob.class, "qz");
		queryHelper.addOrderByProperty("qz.jobState", queryHelper.ORDER_BY_ASC);
		pageResult=qzJobService.getPageResult(queryHelper, super.getPageNo(), super.getPageSize());
		return "listUI";
	}
	
	//跳转到新增页面
	public String addUI(){
		
		ActionContext.getContext().getContextMap().put("jobSendTypeMap", qzJob.JOB_SEND_TYPE_MAP);
		return "addUI";
	}
		
	//保存新增,使用json方式
	public String add(){
		
		Map<String, Object> resultMap=new HashMap<String, Object>();
		if(qzJob!=null){
			
			if(sendExcel!=null){
				List<Integer> no=null;
				//判断是否为excel类型
				if(sendExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
					//判断发送类型
					if(qzJob.getJobSendType()==qzJob.JOB_SEND_TYPE_EMAIL){
						no=qzJobService.checkExcelcheckExcelEmail(sendExcel, sendExcelFileName);
					}else if(qzJob.getJobSendType()==qzJob.JOB_SEND_TYPE_PHONE){
						no=qzJobService.checkExcelcheckExcelPhone(sendExcel, sendExcelFileName);
					}else{
						//没有选择正确的发送类型类型
						resultMap.put("state", 1);
						resultMap.put("msg", "请选择正确的发送类型！");
						result=resultMap;
						return ERROR;
					}
					//no的size大于0，说明有错误行
					if(no.size()>0){
						resultMap.put("state", 2);
						resultMap.put("msg", "请检查excel中发送类型对应格式是否正确！");
						resultMap.put("errorLine", no);
						result=resultMap;
						return ERROR;
					}
					User user=(User) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER);
					//没有错误，把excel文件改名转存到服务器中，并记录路径
					//获得文件后缀名称
					String endFix=sendExcelFileName.substring(sendExcelFileName.lastIndexOf("."));
					//获得upload文件夹路径
					String filePath=ServletActionContext.getServletContext().getRealPath("upload/excel");
					//重命名格式为用户名+&+时间(精确到秒)
					Date date=new Date();
					SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
					String time=dateFormat.format(date);
					
					String newFilename=user.getAccount()+"&"+dateFormat.format(date);
					try {
						FileUtils.copyFile(sendExcel, new File(filePath, newFilename+endFix));
					} catch (IOException e) {
						e.printStackTrace();
					}
					qzJob.setJobName(newFilename);
					qzJob.setJobTable(filePath+"\\"+newFilename+endFix);
					qzJob.setJobCreator(user.getId());
					qzJob.setJobState(qzJob.JOB_STATE_OPEN);
					qzJobService.addJob(qzJob);
					resultMap.put("state", 4);
					resultMap.put("msg", "添加任务完成");
					result=resultMap;
				}else{
					resultMap.put("state", 0);
					resultMap.put("msg", "请上传正确的excel文件");
					result=resultMap;
					return ERROR;
				}
			}else{
				resultMap.put("state", 0);
				resultMap.put("msg", "请上传正确的excel文件");
				result=resultMap;
				return ERROR;
			}
		}else{
			resultMap.put("state", 0);
			resultMap.put("msg", "请填写好任务参数");
			result=resultMap;
			return ERROR;
		}
		return SUCCESS;
	}
	
	public QzJob getQzJob() {
		return qzJob;
	}
	public void setQzJob(QzJob qzJob) {
		this.qzJob = qzJob;
	}
	public List<QzJob> getList() {
		return list;
	}
	public void setList(List<QzJob> list) {
		this.list = list;
	}
	public Object getResult() {
		return result;
	}
	public void setResult(Object result) {
		this.result = result;
	}
	public File getSendExcel() {
		return sendExcel;
	}
	public void setSendExcel(File sendExcel) {
		this.sendExcel = sendExcel;
	}
	public String getSendExcelContentType() {
		return sendExcelContentType;
	}
	public void setSendExcelContentType(String sendExcelContentType) {
		this.sendExcelContentType = sendExcelContentType;
	}
	public String getSendExcelFileName() {
		return sendExcelFileName;
	}
	public void setSendExcelFileName(String sendExcelFileName) {
		this.sendExcelFileName = sendExcelFileName;
	}
	
	
}
